package mythesis.tiles.variable;
import mythesis.gui.main.components.api.TilePopupButton;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.scene.image.*;
import javafx.scene.input.*;


public class If extends CustomNode
{
    var dir = "{{__DIR__}.substring(0, {__DIR__}.length()-15)}resource/";
    var defFill = "#006600";
    var altFill = "#008A00";
    var curFill = defFill;
    
    public var argClick = -1;				// which argument is clicked
 
 	// ----- XML -----   
    public var line    					   = 0;			// where tile is sticked to
    public var args:String[] 			   = ["", "", ""];	// text displayed for each arg box
    public var dataType:Number[]           = [0, 0, 0];	// 1=num, 2=word, 3=truth
    public var dataValue:Number[]          = [0, 0, 0];
    // --- END XML ---
    
    public var allowedDataTypes1:Boolean[] = [true, true, true, false];
    public var allowedDataValue1:Boolean[] = [false, true, false];
    
    public var allowedDataTypes2:Boolean[] = [false, false, false, true];
    public var allowedDataValue2:Boolean[] = [false, false, true];
    
	public var allowedDataTypes3:Boolean[] = [true, true, true, false];
    public var allowedDataValue3:Boolean[] = [true, true, false];
    
    public var specialVariables1:String[] = ["You're not supposed", "to see this. :("];
    public var specialVariables2:String[] = ["You're not supposed", "to see this too. :("];
    public var specialVariables2_Number:String[] = [">=", ">", "==", "<", "<=", "!="];
    public var specialVariables2_Word:String[]   = ["==", "!="];
    public var specialVariables2_Truth:String[]  = ["==", "!="];
    public var specialVariables3:String[] = ["You're not supposed", "to see this too. :("];
    
    public function setDataValue(arg:Integer, dval:Number)
    {
        dataValue[arg] = dval;
    }
    
    public function getDataValue(arg:Number):Number
    {
        if (arg==0)        		{ return dataValue[arg as Integer];	}
        else if (arg==1) 	  	{ return dataValue[arg as Integer];	}
        else if (arg==2)		{ return dataValue[arg as Integer];	}
        else                    { return 0;			}
    }
    
    public function setDataType(arg:Integer, type:Number)
    {
        dataType[arg] = type;
    }
    
    public function getDataType(arg:Number):Number
    {
        if (arg==0)        		{ return dataType[arg as Integer];	}
        else if (arg==1)	   	{ return dataType[arg as Integer];	}
        else if (arg==2)		{ return dataType[arg as Integer];	}
        else                    { return 0;			}
    }
    
    public function getAllowedDataTypes():Boolean[]
    {
        if (argClick==0)		{	return allowedDataTypes1;	}
        else if (argClick==1)	{	return allowedDataTypes2;	}
        else if (argClick==2)	{	return allowedDataTypes3;	}
        else					{	return [false, false, false, false];	}
    }
    
    public function getAllowedDataValues():Boolean[]
    {
        if (argClick==0)		{	return allowedDataValue1;		}
        else if (argClick==1)	{	return allowedDataValue2;		}
        else if (argClick==2)	{	return allowedDataValue3;		}
        else					{	return [false, false, false,false];	}
    }
    
    public function getSpecialVariables():String[]
    {
        if (argClick==0)		{	return specialVariables1;	}
        else if (argClick==1)	{
            if (dataType[0] == 1)
            {
            	return specialVariables2_Number;    
            }
            else if (dataType[0] == 2)
            {
                return specialVariables2_Word;
            }
            else if (dataType[0] == 3)
            {
                return specialVariables2_Truth;
            }
            else
            {
                return [];
            }
            
        }
        else					{	return [""];				}
    }

    var icon = Group
    {
        content:
        [
        	ImageView
        	{
        	    x      : 10
        	    y      : 2
        	    image  : Image
        	    		 {
							url    : "{dir}icoIf.png";
					        width  : 15
					        height : 15
        	    		 }
        	    cache  : true
        	}
        ]
    }
      
    var tile = Group
    {
        content:
        [
        	Rectangle
        	{
        	    width     : 400
        	    height    : 19
        	    arcWidth  : 10
        	    arcHeight : 10
        	    fill      : bind Color.web(curFill)
        	    onMouseEntered: function(e:MouseEvent):Void
        	    				{
        	    				    curFill = altFill;
        	    				}
        	    onMouseExited:  function(e:MouseEvent):Void
        	    				{
        	    				    curFill = defFill;
        	    				}
        	}
        	Rectangle
        	{
        	    width     : 400
        	    height    : 100
        	    opacity   : 0.0
        	    translateX: -50
        	    translateY: -50
        	}
        ]
    }
    
    var tileShadow = Group
    {
        translateX: -1
        translateY: 1
        content:
        [
        	Rectangle
        	{
        	    width     : 400
        	    height    : 19
        	    arcWidth  : 10
        	    arcHeight : 10
        	    fill      : Color.web("#003C00")
        	}
        ]
    }
    
    var text = Group
    {
        translateY: 14
        content:
        [
        	Text
        	{
        	    x       : 35
        	    content : "If"
        	    fill    : Color.WHITE
        	}
			Text
        	{
        	    x       : 210
        	    content : "is"
        	    fill    : Color.WHITE
        	}
        ]
    }
    
    var textShadow = Group
    {
        translateX: -1
        translateY: 15
        content:
        [
        	Text
        	{
        	    x       : 35
        	    content : "If"
        	    fill    : Color.BLACK
        	}
			Text
        	{
        	    x       : 210
        	    content : "is"
        	    fill    : Color.BLACK
        	}
        ]
    }
    
    public var field = Group
    {
        content:
        [
        	TilePopupButton
        	{
        	    translateX : 100 
        	    text       : bind args[0]
        	    color      : 2
        	    onMousePressed: function(e:MouseEvent):Void{
        	        argClick = 0;
        	    }
        	}
        	TilePopupButton
        	{
        	    width	   : 30
        	    translateX : 230 
        	    text       : bind args[1]
        	    color      : 2
        	    onMousePressed: function(e:MouseEvent):Void{
        	        argClick = 1;
        	    }
        	}
        	TilePopupButton
        	{
        	    translateX : 270 
        	    text       : bind args[2]
        	    color      : 2
        	    onMousePressed: function(e:MouseEvent):Void{
        	        argClick = 2;
        	    }
        	}
        ]
    }
    
    
    public var group = Group
    {
        translateY: bind 20*line;
        content:
        [
        	tileShadow,
        	tile,
        	icon,
        	textShadow,
        	text,
        	field
        ]
    }
    
    override public function create():Node
    {
        return group;
    }
}